/**
 * 开单人额度设置 编辑页
 *
 * 作者 zhangwei
 * 日期 2024-08-30 09:28:03
 */
layui.use(['jquery','febs', 'form', 'table','validate','autocomplete'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        $view = $('#incentiveQuotaApply-edit'),
        validate = layui.validate;

    form.verify(validate);
    form.render();

    var tenantId = POPUP_DATA.tenantId;
    var totalQuota;
    initFormValue();
    function initFormValue() {
        if (incentiveQuotaConfigData) {
            form.val("incentiveQuotaApply-edit-form", {
                "tenantId": incentiveQuotaConfigData.tenantId,
                "userId": incentiveQuotaConfigData.userId,
                "userNo": incentiveQuotaConfigData.userNo,
                "realName": incentiveQuotaConfigData.realName,
                "deptId": incentiveQuotaConfigData.deptId,
                "deptName": incentiveQuotaConfigData.deptName
                // "totalQuota": incentiveQuotaConfigData.totalQuota,
                // "rewardedQuota": incentiveQuotaConfigData.rewardedQuota
            });
            totalQuota = incentiveQuotaConfigData.totalQuota == null ? 0:incentiveQuotaConfigData.totalQuota;
            let rewardedQuota = incentiveQuotaConfigData.rewardedQuota == null ? 0:incentiveQuotaConfigData.rewardedQuota;
            let remainQuota = totalQuota - rewardedQuota;
            $("#totalQuota").text(totalQuota + "元");
            $("#rewardedQuota").text(rewardedQuota + "元");
            $("#remainQuota").text(remainQuota + "元");
        }
    }

    form.on('submit(incentiveQuotaApply-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        data.tenantId = tenantId;
        console.log(data);
        if(data.applyQuota <= totalQuota){
            febs.modal.confirm('保存提示', "本次申请额度小于总额度，是否提交申请？",function (index) {
                febs.postArray(ctx + 'incentiveQuotaApply', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                });
            })
        }else{
            febs.modal.confirm('保存提示', "是否提交申请？",function (index) {
                febs.postArray(ctx + 'incentiveQuotaApply', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                });
            })
        }
    }

});